package com.zmx.doctor.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wang.adapters.adapter.BaseAdapterRvList;
import com.wang.adapters.base.BaseViewHolder;
import com.zmx.doctor.R;
import com.zmx.doctor.pojo.OrderInfo;
import com.zmx.doctor.pojo.OrderProduct;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 程序猿：胖胖祥
 * 时 间：2021/3/26  10:32
 * 功 能：
 */

public class OrderManagementAdapter extends BaseAdapterRvList<OrderManagementAdapter.ViewHolder, OrderInfo> {

    private OrderProductAdapter adapter;
    private List<OrderProduct> lists;

    public OrderManagementAdapter(Activity activity) {
        super(activity);
    }

    @Override
    protected void onBindVH(ViewHolder holder, int listPosition, OrderInfo orderInfo) {

        adapter = new OrderProductAdapter(mActivity);
        lists = orderInfo.getLists();
        holder.time.setText(orderInfo.getBuytime());
        holder.reView.setLayoutManager(new LinearLayoutManager(mActivity));
        holder.number.setText("共"+lists.size()+"商品");
        holder.total_money.setText("实际付款：￥"+orderInfo.getTotal());
        holder.reView.setAdapter(adapter);
        adapter.setListAndNotifyDataSetChanged(lists);

        if (orderInfo.getState() == 3){

            holder.cancel_order.setVisibility(View.VISIBLE);
            holder.picking_complete.setVisibility(View.VISIBLE);
            holder.picking_complete.setText("待支付");
            holder.shop_name.setText("待支付");
            holder.picking_complete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    payOnListener.setPayOnListener(listPosition);

                }
            });

            holder.cancel_order.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    payOnListener.setCancelOrder(listPosition);
                }
            });


        }else if(orderInfo.getState() == 0){

            holder.cancel_order.setVisibility(View.GONE);
            holder.picking_complete.setVisibility(View.GONE);
            holder.shop_name.setText("已关闭");
        }else if(orderInfo.getState() == 1){

            holder.cancel_order.setVisibility(View.GONE);
            holder.picking_complete.setVisibility(View.GONE);
            holder.shop_name.setText("已完成");
        }else if(orderInfo.getState() == 2){

            holder.cancel_order.setVisibility(View.GONE);
            holder.picking_complete.setVisibility(View.GONE);
            holder.shop_name.setText("待发货");
        }else{

            holder.cancel_order.setVisibility(View.GONE);
            holder.picking_complete.setVisibility(View.GONE);
            holder.shop_name.setText("以发货");
        }



    }

    @NonNull
    @Override
    protected ViewHolder onCreateVH(ViewGroup parent, LayoutInflater inflater) {

        OrderManagementAdapter.ViewHolder holder = new OrderManagementAdapter.ViewHolder(inflater.inflate(R.layout.adapter_order_management, parent, false));
        return holder;

    }

    static class ViewHolder extends BaseViewHolder {

        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.total_money)
        TextView total_money;
        @BindView(R.id.shop_name)
        TextView shop_name;
        @BindView(R.id.number)
        TextView number;
        @BindView(R.id.reView)
        RecyclerView reView;
        @BindView(R.id.picking_complete)
        Button picking_complete;
        @BindView(R.id.cancel_order)
        Button cancel_order;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    private PayOnListener payOnListener;
    public interface PayOnListener{

        void setPayOnListener(int listPosition);

        void setCancelOrder(int listPosition);

    }

    public void setPayOnListener(PayOnListener payOnListener){
        this.payOnListener = payOnListener;
    }

}

